package com.kaili.basic.scheduler;

import com.kaili.basic.enums.DeviceEunm;
import com.kaili.basic.mapper.DeviceOnlineMapper;
import com.kaili.basic.mapper.DeviceOnlineMapper;
import com.kaili.basic.pojo.entity.MdmDeviceEntity;
import com.kaili.basic.pojo.entity.MdmDeviceSignEntity;
import com.kaili.basic.pojo.entity.MdmDeviceOnline;
import com.kaili.basic.pojo.vo.MdmDeviceSignVO;
import com.kaili.basic.service.IDeviceSignService;
import com.kaili.common.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;

/**
 * @Desc: 签到定时任务
 * @author: zhouyijian
 * @Time: 2019/8/8 14:16
 * @Copyright: © 2018 杭州凯立通信有限公司 版权所有
 * @Warning: 本内容仅限于公司内部传阅, 禁止外泄或用于其它商业目的
 */

public class LoopSignTask implements Runnable{

    @Autowired
    IDeviceSignService deviceSignService;
    @Autowired
    ScheduledExecutorService executor;
    @Autowired
    private DeviceOnlineMapper deviceOnlineMapper;

    public  LoopSignTask(IDeviceSignService deviceSignService, ScheduledExecutorService executor){
        this.executor=executor;
        this.deviceSignService=deviceSignService;
    }

    @Override
    public void run() {
        //查询签到记录
        List<MdmDeviceSignVO> records= deviceSignService.querySignRecord();
        for (MdmDeviceSignVO record:records) {
            //更新签到信息
            MdmDeviceSignEntity entity= new MdmDeviceSignEntity();
            BeanUtils.copyProperties(record,entity);
            entity.setOffTime(record.getSystemTime());
            entity.setOnlineTime(DateUtil.onLineTime(record.getSignTime(),record.getSystemTime()));
            deviceSignService.updateById(entity);
            //更新在线表
            MdmDeviceOnline deviceOnline =new MdmDeviceOnline();
            BeanUtils.copyProperties(record,deviceOnline);
            deviceOnline.setFacilityId(record.getId().longValue());
            deviceOnline.setOendTime(record.getSystemTime());
            deviceOnline.setOnlineStatusId(DeviceEunm.DEVICE_OFFLINE.getCode().longValue());
            deviceOnlineMapper.updateMdmDeviceOnline(deviceOnline);
        }
    }
}
